﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Infrastructure.Data;
using WoChu.Infrastructure.ModelCommmon;
using WoChu.WMS.Repository;

namespace Business.Service
{
    /// <summary>
    /// 用户信息相关操作（ERP用户）
    /// </summary>
    public class UserService
    {
        UserRepository _UserReportRep = new UserRepository();
        public void GetUserList(PageInfo<AppUserList, AppUserList> pageInfo)
        {
            _UserReportRep.GetUserList(pageInfo);
            //密码解密
            pageInfo.Result.ForEach(s => s.PASSWORD = Crypto.DeCrypt(s.PASSWORD));
        }

        public int Insert(AppUserList model)
        {
            if (model == null)
                return -1;
            model.PASSWORD = Crypto.EnCrypt(model.PASSWORD);
            return _UserReportRep.Insert(model);
        }

        public int Update(AppUserList model)
        {
            if (model == null)
                return -1;

            model.PASSWORD = Crypto.EnCrypt(model.PASSWORD);
            return _UserReportRep.Update(model);
        }

        public int Delete(int id)
        {
            if (id < 1)
                return -1;

            return _UserReportRep.Delete(id);
        }

        public bool Exists(AppUserList model)
        {
            List<SqlCondition> conditions = new List<SqlCondition>();
            conditions.Add(new SqlCondition("USERCODE"));

            if (model.USERID > 0)   //若是修改
            {
                conditions.Add(new SqlCondition("USERID", OperateType.NotEqual));
            }
            bool exists = _UserReportRep.Exists(model, conditions);
            return exists;
        }
    }
}
